/*
 * OcchoApp.java
 */

package occho;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.SQLException;
import occho.data.OracleDAOFactory;
import occho.data.UsuarioDAO;
import occho.model.Usuario;
import occho.view.OcchoView;
import org.jdesktop.application.Application;
import org.jdesktop.application.SingleFrameApplication;

/**
 * The main class of the application.
 */
public class OcchoApp extends SingleFrameApplication {

    private Usuario usuario=new Usuario();

    /**
     * At startup create and show the main frame of the application.
     */
    @Override protected void startup() {
        show(new OcchoView(this));
    }
    
    @Override protected void shutdown() {
        occho.data.OracleDAOFactory.closeConnection();
    }

    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     * @param root 
     */
    @Override protected void configureWindow(java.awt.Window root) {
        root.addWindowListener(new WindowAdapter() {

        @Override
        public void windowClosing(WindowEvent e) {
                try {
                    OracleDAOFactory factory = OracleDAOFactory.getInstancia();
                    UsuarioDAO uDAO =factory.getUsuarioDAO();
                    uDAO.setActivo(usuario.getEmail(), false);
                    
                } catch (ClassNotFoundException ex) {}
                  catch (SQLException ex) {}
        }

    });

    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of OcchoApp
     */
    public static OcchoApp getApplication() {
        return Application.getInstance(OcchoApp.class);
    }

    public Usuario getUsuario() {
        return usuario;
    }

    public void setUsuario(Usuario usuario) {
        this.usuario = usuario;
    }

    /**
     * Main method launching the application.
     * @param args 
     */
    public static void main(String[] args) {
        launch(OcchoApp.class, args);
    }
}
